﻿using BlueDot.Data.Core;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServerCe;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SmartDevice.Syncronization.UnitTests.Mocks;
using Syncronization;

namespace SmartDevice.Syncronization.UnitTests
{
    /// <summary>
    ///This is a test class for DeviceSyncAgentTest and is intended
    ///to contain all DeviceSyncAgentTest Unit Tests
    ///</summary>
    [TestClass]
    public class DeviceSyncAgentFixture
    {
        //private TestContext testContextInstance;

        ///// <summary>
        /////Gets or sets the test context which provides
        /////information about and functionality for the current test run.
        /////</summary>
        //public TestContext TestContext
        //{
        //    get
        //    {
        //        return testContextInstance;
        //    }
        //    set
        //    {
        //        testContextInstance = value;
        //    }
        //}

        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #region Tests

        [TestMethod]
        public void CamCreateAgent()
        {
            var syncProvider = new MockSyncProvider();
            var clientProvider = new SqlCeClientSyncProvider("Data Source = Test.sdf", true);

            var agent = new mfLYSyncAgent(syncProvider, clientProvider);

            Assert.AreEqual(clientProvider, agent.LocalProvider);
            Assert.AreEqual(syncProvider, agent.RemoteProvider);
        }

        [TestMethod]
        public void CanAddEntityMap()
        {
            var syncProvider = new MockSyncProvider();
            var clientProvider = new SqlCeClientSyncProvider("Data Source = Test.sdf", true);

            var schemaBuilder1 = new TestSchemaBuilder {Name = "test1"};
            var schemaBuilder2 = new TestSchemaBuilder {Name = "test2"};
            var schemaBuilder3 = new TestSchemaBuilder {Name = "test3"};

            EntityMap.SchemaBuilders.Add("test1", schemaBuilder1);
            EntityMap.SchemaBuilders.Add("test2", schemaBuilder2);
            EntityMap.SchemaBuilders.Add("test3", schemaBuilder3);

            var agent = new mfLYSyncAgent(syncProvider, clientProvider);

            agent.AddSchema(EntityMap.SchemaBuilders, SyncDirection.Bidirectional,
                            TableCreationOption.UploadExistingOrCreateNewTable, "Group1");


            Assert.AreEqual(3, agent.Configuration.SyncTables.Count);

            SyncTable syncTable = agent.Configuration.SyncTables["test1"];

            Assert.AreEqual("test1", syncTable.TableName);
            Assert.AreEqual(SyncDirection.Bidirectional, syncTable.SyncDirection);
            Assert.AreEqual(TableCreationOption.UploadExistingOrCreateNewTable, syncTable.CreationOption);

            Assert.IsNotNull(syncTable.SyncGroup);
            Assert.AreEqual("Group1", syncTable.SyncGroup.GroupName);

            syncTable = agent.Configuration.SyncTables["test2"];

            Assert.AreEqual("test2", syncTable.TableName);
            Assert.AreEqual(SyncDirection.Bidirectional, syncTable.SyncDirection);
            Assert.AreEqual(TableCreationOption.UploadExistingOrCreateNewTable, syncTable.CreationOption);

            Assert.IsNotNull(syncTable.SyncGroup);
            Assert.AreEqual("Group1", syncTable.SyncGroup.GroupName);


            syncTable = agent.Configuration.SyncTables["test3"];

            Assert.AreEqual("test3", syncTable.TableName);
            Assert.AreEqual(SyncDirection.Bidirectional, syncTable.SyncDirection);
            Assert.AreEqual(TableCreationOption.UploadExistingOrCreateNewTable, syncTable.CreationOption);

            Assert.IsNotNull(syncTable.SyncGroup);
            Assert.AreEqual("Group1", syncTable.SyncGroup.GroupName);


            Assert.AreEqual(3, syncProvider.SchemaBuilders.Count);
            Assert.IsTrue(syncProvider.SchemaBuilders.Contains(schemaBuilder1));
            Assert.IsTrue(syncProvider.SchemaBuilders.Contains(schemaBuilder2));
            Assert.IsTrue(syncProvider.SchemaBuilders.Contains(schemaBuilder3));
        }

        [TestMethod]
        public void CanConfigureTables()
        {
            var syncProvider = new MockSyncProvider();
            var clientProvider = new SqlCeClientSyncProvider("Data Source = Test.sdf", true);

            var schemaBuilder1 = new TestSchemaBuilder {Name = "test1"};
            var schemaBuilder2 = new TestSchemaBuilder {Name = "test2"};
            var schemaBuilder3 = new TestSchemaBuilder {Name = "test3"};

            EntityMap.SchemaBuilders.Add("test1", schemaBuilder1);
            EntityMap.SchemaBuilders.Add("test2", schemaBuilder2);
            EntityMap.SchemaBuilders.Add("test3", schemaBuilder3);

            var agent = new mfLYSyncAgent(syncProvider, clientProvider);

            agent.AddSchema(schemaBuilder1, SyncDirection.Bidirectional,
                            TableCreationOption.UploadExistingOrCreateNewTable, "Group1");

            agent.AddSchema(schemaBuilder2, SyncDirection.DownloadOnly,
                            TableCreationOption.DropExistingOrCreateNewTable, "Group1");

            agent.AddSchema(schemaBuilder3, SyncDirection.UploadOnly,
                            TableCreationOption.UploadExistingOrCreateNewTable, "Group2");

            Assert.AreEqual(3, agent.Configuration.SyncTables.Count);

            SyncTable syncTable = agent.Configuration.SyncTables["test1"];

            Assert.AreEqual("test1", syncTable.TableName);
            Assert.AreEqual(SyncDirection.Bidirectional, syncTable.SyncDirection);
            Assert.AreEqual(TableCreationOption.UploadExistingOrCreateNewTable, syncTable.CreationOption);

            Assert.IsNotNull(syncTable.SyncGroup);
            Assert.AreEqual("Group1", syncTable.SyncGroup.GroupName);

            syncTable = agent.Configuration.SyncTables["test2"];

            Assert.AreEqual("test2", syncTable.TableName);
            Assert.AreEqual(SyncDirection.DownloadOnly, syncTable.SyncDirection);
            Assert.AreEqual(TableCreationOption.DropExistingOrCreateNewTable, syncTable.CreationOption);

            Assert.IsNotNull(syncTable.SyncGroup);
            Assert.AreEqual("Group1", syncTable.SyncGroup.GroupName);


            syncTable = agent.Configuration.SyncTables["test3"];

            Assert.AreEqual("test3", syncTable.TableName);
            Assert.AreEqual(SyncDirection.UploadOnly, syncTable.SyncDirection);
            Assert.AreEqual(TableCreationOption.UploadExistingOrCreateNewTable, syncTable.CreationOption);

            Assert.IsNotNull(syncTable.SyncGroup);
            Assert.AreEqual("Group2", syncTable.SyncGroup.GroupName);
        }

        #endregion
    }
}